package com.gy.api.pc.content.service.impl;

import com.gy.api.base.Type;
import com.gy.api.pc.content.dao.PosterDao;
import com.gy.api.pc.content.domain.po.PosterPO;
import com.gy.api.pc.content.domain.vo.PosterAddVO;
import com.gy.api.pc.content.domain.vo.PosterSelectListDto;
import com.gy.api.pc.content.domain.vo.PosterUpdateVO;
import com.gy.api.pc.content.domain.vo.PosterVO;
import com.gy.api.pc.content.service.IPosterService;
import com.gy.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


@Service
@Transactional
@Slf4j
public class PosterServiceImpl implements IPosterService {

    @Autowired
    PosterDao posterDao;
    @Override
    public List<PosterVO> selectPosterList(PosterSelectListDto poster) {
        return posterDao.selectPosterList(poster);
    }

    @Override
    public PosterVO selectPosterById(String id) {
        PosterPO posterPO=posterDao.selectByPrimaryKey(id);
        if(posterPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        PosterVO posterVO=new PosterVO();
        BeanUtils.copyProperties(posterPO, posterVO);
        return posterVO;
    }


    @Override
    public void updatePosterEnable(String enable, String id) {
        PosterPO posterPO=posterDao.selectByPrimaryKey(id);
        if(posterPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        posterPO.setEnable(enable);
        posterPO.preUpdate();
        posterDao.updateByPrimaryKeySelective(posterPO);
    }

    @Override
    public void updatePoster(PosterUpdateVO updateDto) {
        PosterPO posterPO=posterDao.selectByPrimaryKey(updateDto.getId());
        if(posterPO==null){
            throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
        }
        BeanUtils.copyProperties(updateDto,posterPO);
        posterPO.preUpdate();
        posterDao.updateByPrimaryKeySelective(posterPO);
    }

    @Override
    public void insertPoster(PosterAddVO addVO) {
        PosterPO posterPO=new  PosterPO();
        BeanUtils.copyProperties(addVO,posterPO);
        posterPO.preInsert();
        posterDao.insertSelective(posterPO);
    }

    @Override
    public void deletePosterByIds(List<String> ids) {
        for (String id:ids) {
            PosterPO posterPO=posterDao.selectByPrimaryKey(id);
            if(posterPO==null){
                throw new BusinessException(Type.FAIL.getCode(),"信息未找到！");
            }
            posterPO.preDelete();
            posterDao.updateByPrimaryKeySelective(posterPO);
        }
    }
}